import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 重写路由push 解决警告 可以不写
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(err => err)
}

import Login from '@/views/login/Login.vue'
import Layout from '@/views/layout/Layout.vue'
import { loacl } from '@/utils/local'

const routes = [
    // 登录
    {
        path: '/login',
        component: Login
    },
    // 后台首页
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        meta: { path: '/home', title: '后台首页' },
        children: [{
            path: '/home',
            component: () =>
                import ('@/views/home/Home.vue')
        }]
    },
    // 账号管理
    {
        path: '/account',
        component: Layout,
        redirect: '/account/account-list',
        meta: { path: '/account', title: '账号管理' },
        children: [{
                path: '/account/account-list',
                component: () =>
                    import ('@/views/account/AccountList.vue'),
                meta: { path: '/account/account-list', title: '账号列表' }
            },
            {
                path: '/account/account-add',
                component: () =>
                    import ('@/views/account/AccountAdd.vue'),
                meta: { path: '/account/account-add', title: '账号添加' }
            },
            {
                path: '/account/password-modify',
                component: () =>
                    import ('@/views/account/PasswordModify.vue'),
                meta: { path: '/account/password-modify', title: '修改密码' }
            },
            {
                path: '/account/personal',
                component: () =>
                    import ('@/views/account/Personal.vue'),
                meta: { path: '/account/personal', title: '个人中心' }
            }
        ]
    },
    // 商品管理
    {
        path: '/goods',
        component: Layout,
        redirect: '/goods/goods-list',
        meta: { path: '/goods', title: '商品管理' },
        children: [{
                path: '/goods/goods-add',
                component: () =>
                    import ('@/views/goods/GoodsAdd.vue'),
                meta: { path: '/goods/goods-add', title: '商品添加' }
            },
            {
                path: '/goods/goods-list',
                component: () =>
                    import ('@/views/goods/GoodsList.vue'),
                meta: { path: '/goods/goods-list', title: '商品列表' }
            },
            {
                path: '/goods/goods-cate',
                component: () =>
                    import ('@/views/goods/GoodsCate.vue'),
                meta: { path: '/goods/goods-cate', title: '商品分类' }
            }
        ]
    },
    // 订单管理
    {
        path: '/order',
        component: Layout,
        meta: { path: '/order', title: '订单管理' },
        children: [{
            path: '',
            component: () =>
                import ('@/views/order/Order.vue')
        }]
    },
    // 店铺管理
    {
        path: '/shop',
        component: Layout,
        meta: { path: '/shop', title: '店铺管理' },
        children: [{
            path: '',
            component: () =>
                import ('@/views/shop/Shop.vue')
        }]
    },
    // 销售统计
    {
        path: '/total',
        component: Layout,
        redirect: '/total/goods-total',
        meta: { path: '/total', title: '销售统计' },
        children: [{
                path: '/total/goods-total',
                component: () =>
                    import ('@/views/total/GoodsTotal.vue'),
                meta: { path: '/total/goods-total', title: '商品统计' }
            },
            {
                path: '/total/order-total',
                component: () =>
                    import ('@/views/total/OrderTotal.vue'),
                meta: { path: '/total/order-total', title: '订单统计' }
            }
        ]
    }
]

const router = new VueRouter({
        routes
    })
    // 全局前置路由守卫
router.beforeEach((to, from, next) => {
    let isLogin = loacl.get('token_userlogin') ? true : false
    if (isLogin) {
        next()
    } else if (to.path === '/login') {
        next()
    } else {
        next({ path: '/login' })
    }
})
export default router